﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using jaysJourney_v01.Common;
using jaysJourney_v01.Utils;



namespace jaysJourney_v01.States
{
    class MenuState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        private SpriteBatch mainSpriteBatch;

        private InputGroup menuButtonGroup;

        private Texture2D interfaceBG;
        private Texture2D userdataBG;
        private Texture2D xpFrame;
        private Texture2D xpFill;

        // Buttons
        private Button createWorld;
        private Button loadWorld;
        private Button tutorial;
        private Button settings;
        private Button logout;
        private Button exit;

        /**
         * METHODS
         * */

        // Constructor

        public MenuState(String name, StateManager stateManager) : base(name, stateManager) 
        {
           
        }

        // Initialize - Called only once

        public override void initialize()
        {
            mainSpriteBatch = this.parentSpriteBatch;

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\main_menu_v2");
            this.userdataBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\userdata_bg");
            this.xpFrame = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\xp_frame");
            this.xpFill = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\xp_fill");

            // Buttons
            this.createWorld = new Button(this, "CREATE WORLD", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 350, Color.White, false, true);
            this.loadWorld = new Button(this, "LOAD WORLD", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 450, Color.White, false, true);
            this.tutorial = new Button(this, "TUTORIAL", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 550, Color.White, false, true);
            this.settings = new Button(this, "SETTINGS", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 650, Color.White, false, true);
            this.logout = new Button(this, "LOGOUT", @"Textures\Interface\back_btn", @"Textures\Interface\back_btn_ac", 94, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);
            this.exit = new Button(this, "EXIT", @"Textures\Interface\quit_btn", @"Textures\Interface\quit_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);

            this.menuButtonGroup.addButton(createWorld);
            this.menuButtonGroup.addButton(loadWorld);
            this.menuButtonGroup.addButton(tutorial);
            this.menuButtonGroup.addButton(settings);
            this.menuButtonGroup.addButton(logout);
            this.menuButtonGroup.addButton(exit);
        }

        // MAIN UPDATE LOOP

        public override void update(GameTime gameTime)
        {

            Button click = this.menuButtonGroup.update(gameTime);

            if (click != null && click.getName() == "EXIT") this.getGame().Exit();
            else if (click != null && click.getName() == "CREATE WORLD") this.getStateManager().setActiveState("editorState");
            else if (click != null && click.getName() == "LOAD WORLD")
            {
                ((LoadWorldState)this.getStateManager().getState("loadWorldState")).loadworldoverview = true;
                this.getStateManager().setActiveState("loadWorldState");
            }
            else if (click != null && click.getName() == "TUTORIAL") this.getStateManager().setActiveState("tutorialState");
            else if (click != null && click.getName() == "SETTINGS") this.getStateManager().setActiveState("settingsState");
            else if (click != null && click.getName() == "LOGOUT")
            {
                this.getGame().activePlayer.clearData();
                this.getStateManager().setActiveState("loginState");
            }
        }

        // MAIN DRAW METHOD

        public override void draw(GameTime gameTime)
        {
            // bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // draw interface bg
            this.mainSpriteBatch.Draw(this.userdataBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) + 170, 350, this.userdataBG.Width, this.userdataBG.Height), new Rectangle(0, 0, this.userdataBG.Width, this.userdataBG.Height), Color.White);
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // buttons
            this.menuButtonGroup.draw(gameTime);

            // curPlayer
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "Currently logged in with:\nUser: " + this.getGame().activePlayer.getUsername() + "\nMoney: " + this.getGame().activePlayer.getMoney() + "\nLevel: " + this.getGame().activePlayer.getCurrentLevel(), new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2 + 215, 360), Color.White);
            this.mainSpriteBatch.Draw(this.xpFrame, new Rectangle(this.getGame().GraphicsDevice.Viewport.Width / 2 + 330, 440, xpFrame.Width*80/100, xpFrame.Height), Color.White);
            this.mainSpriteBatch.Draw(this.xpFill, new Rectangle(this.getGame().GraphicsDevice.Viewport.Width / 2 + 330, 440, xpFill.Width*getGame().activePlayer.progress/100*80/100, xpFrame.Height), Color.White);
        }

        public override void onEnterState()
        {
            
            this.createWorld.setNewPosition(250);
            this.loadWorld.setNewPosition(350);
            this.tutorial.setNewPosition(450);
            this.settings.setNewPosition(550);
            this.logout.setNewPosition(94, this.getGame().GraphicsDevice.Viewport.Height - 74);
            this.exit.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }
    }
}
